// 加载FORM表单扩展
layui.use(['form'], function(){
    var form = layui.form()
        ,layer = layui.layer;
	
	// 设置图片宽高
	function setImageSize(imageURL) {
		// 创建对象
		var image = new Image();
		 
		// 改变图片的src
		image.src = imageURL
		// 判断是否有缓存
		if(image.complete){
			$(".ad_width").val(image.width);
			$(".ad_height").val(image.height);
		}else{
			// 加载完成执行
			image.onload = function(){
				$(".ad_width").val(image.width);
				$(".ad_height").val(image.height);
			};
		}
	}
	
	// 初始化上传组件
	var uploader = new plupload.Uploader({
		browse_button : 'uploadImage',
		url : '/upload/imageOne',
		flash_swf_url : 'js/Moxie.swf',
		silverlight_xap_url : 'js/Moxie.xap',
		multi_selection: false,
		filters: {
		  mime_types : [ //只允许上传图片文件和rar压缩文件
		    { title : "图片文件", extensions : "jpg,gif,png,bmp" }
		  ],
		  max_file_size : '500kb',
		  prevent_duplicates : true //不允许队列中存在重复文件
		}
	});
	
	uploader.init(); //初始化
  
	//绑定文件添加进队列事件
	uploader.bind('FilesAdded',function(uploader,files){	
		uploader.start();
	});

	//绑定文件上传进度事件
	uploader.bind('UploadProgress',function(uploader,file){
		$("#uploadImage").text('上传中 ' + file.percent + '%' );
	});
	
	uploader.bind('FileUploaded',function(uploader,file,responseObject){

		if( responseObject.status == 200 ) {
			var response = JSON.parse(responseObject.response);
			$(".preview-content").html("<img src='"+ response.url +"'>");
			$(".game_logo").val(response.url);
			
			setImageSize( response.url );
		}
		
		$("#uploadImage").text('重新上传');
	});
	
  
    //自定义验证规则
    form.verify({
        game_name: function(value){
            if(+value < 1) {
                return '游戏物料名称不能为空';
            }

        }
        ,application_channel: function(value){
            if(value.length < 1) {
                return '应用渠道不能为空';
            }
        }
        ,game_describe: function(value){
            if(value.length < 1) {
                return '游戏描述不能为空';
            }

        }
       
		,image_url: function(value){
			if(value.length < 1) {
                return '请上传图片文件';
            }
		}
        ,g_id: function(value){
            if(value.length < 1) {
                return '游戏id不能为空';
            }
        }
        
		,ad_width: function(value){
			if(+value < 1) {
                return '请输入图片宽度';
            }
		}
		,ad_height: function(value){
			if(+value < 1) {
                return '请输入图片高度';
            }
		}
    });
   
    //监听提交
    form.on('submit(submit)', function(data){
        $.ajax({
            url: "/adGame/create",
            type: "post",
            data: data.field ,
            dataType: "json",
            success: function (response) {
                if(response.status == 'success') {
                    PopModel.refreshParentFrame();
                } else {
                    layer.msg(response.msg, {icon: 5, shift: 6}, function(){});
                }
            }
        });
    });

	// 刷新表单
	form.render();
});
